package com.handa.documentManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.handa.common.config.SnowflakeConfig;
import com.handa.documentManager.dao.DocumentUserBucketDao;
import com.handa.documentManager.entity.DO.DocumentUserBucketDO;
import com.handa.documentManager.service.DocumentUserBucketService;
import com.handa.documentManager.utils.BucketNameGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (DocumentUserBucket)表服务实现类
 *
 * @author makejava
 * @since 2025-09-14 21:08:34
 */
@Service("documentUserBucketService")
public class DocumentUserBucketServiceImpl extends ServiceImpl<DocumentUserBucketDao, DocumentUserBucketDO> implements DocumentUserBucketService {

    @Resource
    private BucketNameGenerator bucketNameGenerator;

    @Resource
    private SnowflakeConfig snowflakeConfig;

    @Override
    public DocumentUserBucketDO getDocumentUserBucket(String userId) {
        QueryWrapper<DocumentUserBucketDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        DocumentUserBucketDO res = this.getOne(queryWrapper);
        if (res == null) {
            DocumentUserBucketDO tmp = new DocumentUserBucketDO();
            tmp.setId(snowflakeConfig.snowflakeId());
            tmp.setUserId(Long.parseLong(userId));
            tmp.setBucketName(bucketNameGenerator.generateBucketName());
            this.save(tmp);
            return tmp;
        }
        return res;
    }
}

